using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node cast_expression in the grammar(AnsiC).
    /// </summary>
    internal partial class Vncast_expression : IFullFormatter {
        // [026]=cast_expression : unary_expression ;
        // [027]=cast_expression : '(' type_name ')' cast_expression ;
        private readonly Vnunary_expression last0;
        private readonly List<Token> list0 = new();
        private readonly List<Vntype_name> list1 = new();
        private readonly List<Token> list2 = new();

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            var count = this.list0.Count;
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);

            for (var i = 0; i < count; i++) {
                var left = this.list0[i];
                var typeName = this.list1[i];
                var right = this.list2[i];
                context.PrintBlanksAnd(left, i == 0 ? preConfig : config, writer);
                context.PrintCommentsBetween(left, typeName, config, writer);
                context.PrintBlanksAnd(typeName, config, writer);
                context.PrintCommentsBetween(typeName, right, config, writer);
                context.PrintBlanksAnd(right, config, writer);
                if (i + 1 < count) {
                    context.PrintCommentsBetween(right, this.list0[i + 1], config, writer);
                }
                else {
                    context.PrintCommentsBetween(right, this.last0, config, writer);
                }
            }
            {
                context.PrintBlanksAnd(this.last0, count > 0 ? config : preConfig, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }


        // [26]=cast_expression : unary_expression ;
        // dumped by ReversedListExtractor 1
        internal Vncast_expression(Vnunary_expression last0) {
            this.last0 = last0;
            this._scope = new TokenRange(last0);
        }
        // [27]=cast_expression : '(' type_name ')' cast_expression ;
        // dumped by ReversedListExtractor 2
        internal void Insert(
            Token r3/* reserved word is omissible */
            , Vntype_name r2
            , Token r1/* reserved word is omissible */
        ) {
            this.list0.Insert(0, r3);
            this.list1.Insert(0, r2);
            this.list2.Insert(0, r1);
            this._scope.start = r3.index;
        }

    }
}
